import {Component, OnInit} from '@angular/core';
import {HttpParams} from '@angular/common/http';
import {Router} from '@angular/router';
import {timer} from 'rxjs';
import {NzModalService} from 'ng-zorro-antd';
import {AppService} from '../../service/app/app.service';

@Component({
  selector: 'app-user',
  templateUrl: './user.component.html',
  styleUrls: ['./user.component.css']
})
export class UserComponent implements OnInit {
  userInfo: any;
  signButtonText = '签到';
  vipInfo = 'vip';
  constructor(private appService: AppService,
              private modalService: NzModalService,
              private router: Router) { }

  ngOnInit() {
    this.getUserInfo();
  }
  // 获取邀请码
  getInterCode() {
    this.appService.get(this.appService.urls.invite + localStorage.getItem('user_id'), {observe: 'response', params: new HttpParams().append('access_token', localStorage.getItem('token'))}).subscribe((resp: any) => {
      if (resp.body.ret === 1) {
        this.modalService.success({
          nzTitle: '提示',
          nzContent: '您的邀请码是： '　+　resp.body.data.code,
          nzOkText: '确定',
        });
      }
    });
  }
  getUserInfo() {
    this.appService.get(this.appService.urls.userinfo + localStorage.getItem('user_id'), {observe: 'response', params: new HttpParams().append('access_token', localStorage.getItem('token'))}).subscribe((resp: any) => {
      this.userInfo = resp.body;
      switch (this.userInfo.class) {
        case 1:
          this.vipInfo = 'vip';
          break;
        case 2:
          this.vipInfo = 'svip';
          break;
        case 3:
          this.vipInfo = 'ssvip';
          break;
      }
      if (!this.userInfo.data.is_checkin) {
        this.signButtonText = '已签到';
      }
    });
  }
  back() {
    this.appService.get(this.appService.urls.logout + localStorage.getItem('user_id'), {observe: 'response', params: new HttpParams().append('access_token', localStorage.getItem('token'))}).subscribe((resp: any) => {
      if (resp.body.ret === 1) {
        localStorage.setItem('email', null);
        localStorage.setItem('passwd', null);
        localStorage.setItem('token', null);
        localStorage.setItem('user_id', null);
        localStorage.setItem('status', 'off');
        localStorage.setItem('vmess', null);
        this.router.navigate(['login']).catch();
      }
    });
  }
  backToHome() {
    this.router.navigate(['home']).catch();
  }
  charge() {
    this.router.navigate(['charge']).catch();
  }
  sign() {
    this.appService.get(this.appService.urls.sign + localStorage.getItem('user_id'), {observe: 'response',
      params: new HttpParams().append('access_token', localStorage.getItem('token'))}).subscribe((resp: any) => {
      const signInfo = resp.body;
      const option = {
        nzTitle: '签到结果',
        nzContent: signInfo.msg
      };
      let modal;
      if (signInfo.ret === 1) {
        this.signButtonText = '已签到';
        modal = this.modalService.success(option);
      } else {
        this.signButtonText = '已签到';
        modal = this.modalService.warning(option);
      }
      timer(2000).subscribe(() => {
        modal.destroy();
      });
    });
  }
}
